using Net;
using System.Collections.Generic;
using System.IO;

/// <summary>
///   //心跳响应消息
/// </summary>
public enum MessageId
{
    lobby_cmd_id_null = 0,
    lobby_cmd_id_u2ls_user_register = 1,
    lobby_cmd_id_u2ls_user_login = 2,
    lobby_cmd_id_ls2u_user_login = 3,
    lobby_cmd_id_u2ls_user_update_info = 4,
    lobby_cmd_id_u2ls_user_heart_beat = 5,
    lobby_cmd_id_ls2u_user_heart_beat = 6,
}

public class cmd : BaseMessage
{
    public override short MsgId => 0;
    public int id;  //消息id（对应接口id）
    private readonly uint tag_id = ProtoHelper.CalcTag(1, "int32");
    public byte[] data;  //数据
    private readonly uint tag_data = ProtoHelper.CalcTag(2, "bytes");
    public string sign;  // 需要签名的接口（只有少部分才使用sign签名）
    private readonly uint tag_sign = ProtoHelper.CalcTag(3, "string");
    public int code;  //code 响应码（消息返回时，才有此字段，0与200为正常状态，其他为错误消息id）
    private readonly uint tag_code = ProtoHelper.CalcTag(4, "int32");

    public override void Deserialize(byte[] data,ref int offset)
    {
        int newOffset = offset;
        newOffset = ProtoHelper.ReadRawTagInPlace(data, offset, out uint tag_read_id);
        if (tag_id == tag_read_id)
        {
            offset = newOffset;
            id = ProtoHelper.ReadInt32(data, ref offset);
        }

        newOffset = ProtoHelper.ReadRawTagInPlace(data, offset, out uint tag_read_data);
        if (tag_data == tag_read_data)
        {
            offset = newOffset;
            data = ProtoHelper.ReadBytes(data, ref offset);
        }

        newOffset = ProtoHelper.ReadRawTagInPlace(data, offset, out uint tag_read_sign);
        if (tag_sign == tag_read_sign)
        {
            offset = newOffset;
            sign = ProtoHelper.ReadString(data, ref offset);
        }

        newOffset = ProtoHelper.ReadRawTagInPlace(data, offset, out uint tag_read_code);
        if (tag_code == tag_read_code)
        {
            offset = newOffset;
            code = ProtoHelper.ReadInt32(data, ref offset);
        }

    }

    public override byte[] Serialize()
    {
        byte[] __bytes;
        using MemoryStream ms = new MemoryStream();
        using BinaryWriter bw = new BinaryWriter(ms);

        __bytes = ProtoHelper.WriteInt32(id);
        {
            bw.Write(ProtoHelper.WriteTag(1, "int32"));
            bw.Write(__bytes);
        }

        __bytes = ProtoHelper.WriteBytes(data);
        if (__bytes != null && __bytes.Length > 0)
        {
            bw.Write(ProtoHelper.WriteTag(2, "bytes"));
            bw.Write(__bytes);
        }

        __bytes = ProtoHelper.WriteString(sign);
        if (__bytes != null && __bytes.Length > 0)
        {
            bw.Write(ProtoHelper.WriteTag(3, "string"));
            bw.Write(__bytes);
        }

        __bytes = ProtoHelper.WriteInt32(code);
        {
            bw.Write(ProtoHelper.WriteTag(4, "int32"));
            bw.Write(__bytes);
        }

        return ms.ToArray();
    }
}

